package com.qbw.app.module.vehicle.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.http.okhttp.HttpTask;
import com.lib.util.StringUtil;
import com.qbw.app.Constant;
import com.qbw.app.R;
import com.qbw.app.databinding.VehicleActivityEstimateDistanceBinding;
import com.qbw.app.model.EstimateDistance;
import com.qbw.app.model.Vehicle;
import com.qbw.app.module.common.activity.CommonActivity;
import com.qbw.app.module.common.util.HttpRequestUtil;
import com.qbw.app.module.common.util.UniversalToastUtil;
import com.qbw.app.module.common.view.PieChart;
import com.qbw.customview.RefreshLoadMoreLayout;

import java.util.ArrayList;
import java.util.List;

import static com.qbw.app.Constant.KEY_VEHICLE;
import static com.qbw.app.Constant.KEY_VEHICLE_CODE;
import static com.qbw.app.request.impl.VehicleRequest.METHOD_ESTIMETE_DISTANCE;

/**
 * 预估里程
 *
 * @author QBW
 * @date 2019/8/23
 */
public class VehicleEstimateDistanceActivity extends CommonActivity<VehicleActivityEstimateDistanceBinding> {

    //private String mVehicleCode;
    private Vehicle mVehicle;

    public static void start(Context context, Vehicle vehicle) {
        Intent intent = new Intent(context, VehicleEstimateDistanceActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        intent.putExtra(KEY_VEHICLE, vehicle);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.vehicle_activity_estimate_distance;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = savedInstanceState == null ? getIntent().getExtras() : savedInstanceState;
        mVehicle = (Vehicle) args.getSerializable(KEY_VEHICLE);
        mBinding.title.setSubTitle(mVehicle.getVehicleNick());
        mBinding.title.setSubTitleVisible(true);
        mBinding.title.setListener(this);
        mBinding.rll.init(new RefreshLoadMoreLayout.Config(this).canLoadMore(false));

        mGeneralManager.getRequestManager()
                       .getVehicleRequest()
                       .getEstimeteDistance(mVehicle.getVehicleCode(), this)
                       .execute();



        /*IKnowDialogFragment.createInstance(ResUtil.getString(R.string.estimete_distance_iknow_tip))
                           .show(getSupportFragmentManager(), "iknow");
        IKnowDialogFragment.createInstance(Gravity.CENTER,
                                           String.format(ResUtil.getString(R.string.estimete_distance_iknow_oil_electric_tip_format),
                                                         "1111"))
                           .show(getSupportFragmentManager(), "iknow1");*/
    }

    @Override
    public void onRefresh() {
        mGeneralManager.getRequestManager()
                       .getVehicleRequest()
                       .getEstimeteDistance(mVehicle.getVehicleCode(), this)
                       .execute();
    }

    @Override
    public void onHttpSuccess(HttpTask httpTask, Object entity) {
        super.onHttpSuccess(httpTask, entity);
        String method = httpTask.getMethod();
        if (method.equals(METHOD_ESTIMETE_DISTANCE)) {
            EstimateDistance.Response response = (EstimateDistance.Response) entity;
            EstimateDistance estimateDistance = response.getData();
            mBinding.setEstimate(estimateDistance);
            if (estimateDistance.getState() == Constant.ESTIMATE_DISTANCE_STATE_NORMAL) {
                updateUi(estimateDistance);
            }
            mBinding.executePendingBindings();
            mBinding.rll.stopRefresh();
        }
    }

    @Override
    public void onHttpFailed(HttpTask httpTask, int errorCode, String message) {
        super.onHttpFailed(httpTask, errorCode, message);
        message = HttpRequestUtil.getMessage(errorCode, message);
        String method = httpTask.getMethod();
        if (method.equals(METHOD_ESTIMETE_DISTANCE)) {
            UniversalToastUtil.showFailedIconToast(message);
            mBinding.rll.stopRefresh();
        }
    }

    private void updateUi(EstimateDistance estimateDistance) {
        List<PieChart.PieEntry> pieEntries = new ArrayList<>();
        int remainderOil = (int) StringUtil.numberFloat(estimateDistance.getResidualFuel());
        PieChart.PieEntry pieEntry = new PieChart.PieEntry(100 - remainderOil,
                                                           R.color.vehicle_yugu_licheng_shiyong,
                                                           false);
        pieEntries.add(pieEntry);
        pieEntries.add(new PieChart.PieEntry(remainderOil, R.color.vehicle_yugu_licheng_shengyu, true));
        mBinding.pie.setPieEntries(pieEntries);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable(KEY_VEHICLE, mVehicle);
    }
}
